//
//  FJGuidanceViewController.swift
//  FruitJuice
//
//  Created by Rui on 2023/12/20.
//

import UIKit
import JXBanner
import JXPageControl

class FJGuidanceViewController: FJBaseViewController {

    private let imgDataArr: [String] = ["FJ_Guide_one","FJ_Guide_two"]
    private lazy var linearBanner: JXBanner = {[weak self] in
        let banner = JXBanner()
        banner.placeholderImgView.image = UIImage(named: "Learning_Welcome_one")
        banner.indentify = "linearBanner"
        banner.delegate = self
        banner.dataSource = self
        return banner
    }()
    private var currentIndex = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            linearBanner
        }
        
        linearBanner.fillContainer()
        UserDefaults.standard.set("in", forKey: "InApp")
        UserDefaults.standard.synchronize()
    }
    

}

extension FJGuidanceViewController: JXBannerDataSource, JXBannerDelegate {

    func jxBanner(numberOfItems banner: JXBannerType) -> Int {
        return imgDataArr.count
    }

    func jxBanner(_ banner: JXBannerType) -> (JXBannerCellRegister) {

        return JXBannerCellRegister(type: FJBannerCell.self,
                                        reuseIdentifier: "FJBannerCell")
    }

    func jxBanner(_ banner: JXBannerType,
                  cellForItemAt index: Int,
                  cell: UICollectionViewCell) -> UICollectionViewCell {

        guard let imageCell = cell as? FJBannerCell else {
            return cell
        }
        imageCell.imgString = imgDataArr[index]
        return imageCell
    }

    func jxBanner(_ banner: JXBannerType,
                  params: JXBannerParams) -> JXBannerParams {

        return params
            .timeInterval(2)
            .cycleWay(.forward)
            .isAutoPlay(false)
    }

//    func jxBanner(_ banner: JXBannerType,
//                  layoutParams: JXBannerLayoutParams)
//        -> JXBannerLayoutParams {
//
//        return layoutParams
//            .layoutType(JXBannerTransformLinear())
//            .itemSize(CGSize(width: .screenWidth, height: 350))
//            .itemSpacing(10)
//            .rateOfChange(0.8)
//            .minimumScale(0.7)
//            .rateHorisonMargin(0.5)
//            .minimumAlpha(0.8)
//    }

    func jxBanner(pageControl banner: JXBannerType,
                  numberOfPages: Int,
                  coverView: UIView,
                  builder: JXBannerPageControlBuilder) -> JXBannerPageControlBuilder {

        let pageControl = JXPageControlScale()
        pageControl.contentMode = .bottom
        pageControl.activeSize = CGSize(width: 15, height: 6)
        pageControl.inactiveSize = CGSize(width: 6, height: 6)
        pageControl.activeColor = .white
        pageControl.inactiveColor = .white.alpha(0.4)
        pageControl.columnSpacing = 0
        pageControl.isAnimation = true
        builder.pageControl = pageControl
        builder.layout = {
            pageControl.snp.makeConstraints { (maker) in
                maker.left.equalTo(coverView).offset(30)
                maker.bottom.equalTo(coverView.snp.bottom).offset(-68)
                maker.height.equalTo(20)
            }
        }
        return builder

    }
    
    // 最中心显示cell 索引
    func jxBanner(_ banner: JXBannerType, center index: Int) {
        currentIndex += 1
        if currentIndex > 2 {
            UIApplication.window?.rootViewController = FJNavViewController(rootViewController: FJWelcomeViewController())
        }
    }
}


class FJBannerCell: JXBannerBaseCell {

    private lazy var bannarImg = c_make_ex(UIImageView.self).mode(.scaleAspectFill).end()


    var imgString: String = "" {
        didSet {
            bannarImg.image = imgString.toImage
        }
    }
    
    var imgUrl: String = "" {
        didSet {
            bannarImg.loadImage(url: imgUrl)
        }
    }

    open override func jx_addSubviews() {
        super.jx_addSubviews()
        contentView.subviews{
            bannarImg
        }

        bannarImg.fillContainer()
    }
}
